/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Country;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LeThuyLinh
 */
public class DALtbl_Country extends BaseConnection{
    CallableStatement cstm=null;
    ResultSet rs=null;
    public boolean Addtbl_Country(Etbl_Country obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Country_add(?,?)}");
            cstm.setString("name", obj.getName());
            cstm.setString("description", obj.getDescription());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean updatetbl_Country(Etbl_Country obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Country_update(?,?,?)}");
            cstm.setInt("country_Id", obj.getCountry_ID());
            cstm.setString("name", obj.getName());
            cstm.setString("description", obj.getDescription());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean deletetbl_Country(int country_ID){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Country_delete(?)}");
            cstm.setInt("country_Id", country_ID);
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public Etbl_Country getCountry(int countryID){
        Etbl_Country objCountry=null;
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Country_get(?)}");
            cstm.setInt("country_Id", countryID);
            rs=cstm.executeQuery();
            while(rs.next()){
                objCountry=new Etbl_Country();
                objCountry.setCountry_ID(rs.getInt("country_Id"));
                objCountry.setName(rs.getString("name"));
                objCountry.setDescription(rs.getString("description"));
                return objCountry;
            }
            cstm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
     public ArrayList getListCountry(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        try {
            ArrayList countryList=new ArrayList();
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Country_getpaged(?,?,?,?,?)}");
            cstm.setString("OrderBy", orderBy);
            cstm.setString("TangGiam", ASCorDESC);
            cstm.setString("WhereClause", WhereClause);
            cstm.setInt("PageIndex", PagingIndex);
            cstm.setInt("PageSize", PagingSize);
            rs=cstm.executeQuery();
            DALTotalPageIndex.getTotalPage("tbl_Country", WhereClause, PagingSize);
            while(rs.next()){
                Etbl_Country objCountry=new Etbl_Country();
                objCountry.setCountry_ID(rs.getInt("country_Id"));
                objCountry.setName(rs.getString("country_name"));
                objCountry.setDescription(rs.getString("description"));
                countryList.add(objCountry);
            }
            cstm.close();
            rs.close();
            releaseConnection();
            return countryList;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
